package com.aabte.auth.account.resource.api;

import com.aabte.auth.account.resource.api.role.CreateRoleCommand;
import com.aabte.auth.account.resource.api.role.RoleApi;
import com.aabte.auth.account.resource.facade.RoleServiceFacade;
import com.aabte.auth.role.domain.exception.RoleNotExistException;
import com.aabte.commons.bean.BaseResponse;
import com.aabte.commons.errorcode.ResponseFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Slf4j
@ControllerAdvice
@Validated
@RestController
public class RoleController implements RoleApi {

    @Resource
    private RoleServiceFacade roleServiceFacade;

    @Override
    public BaseResponse<String> create(@Valid CreateRoleCommand command) {
        return ResponseFactory.success(roleServiceFacade.create(command.getRoleName()));
    }

    @Override
    public BaseResponse<Boolean> delete(String roleId) {
        try {
            return ResponseFactory.success(roleServiceFacade.delete(roleId));
        } catch (RoleNotExistException e) {
            throw RoleCode.ROLE_NOT_EXIST.newException(e);
        }
    }

}
